package com.abl.lib.common.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * 拼音工具类
 * 支持： 汉字转拼音（携带分隔符、携带音标、取汉字首字母）
 */
public class PinyinKit {

    //汉字转拼音
    public static String toPinYin(String chineseStr) {
        return toPinYin(chineseStr, false, false, null);
    }

    //汉字转拼音（携带分隔符）
    public static String toPinYin(String chineseStr, String delimiter) {
        return toPinYin(chineseStr, false, false, delimiter);
    }

    //只保留汉字拼音
    public static String toPinYinOnlyChinese(String chineseStr) {
        return toPinYin(chineseStr, false, true, null);
    }

    //只保留汉字拼音（携带分隔符）
    public static String toPinYinOnlyChinese(String chineseStr, String delimiter) {
        return toPinYin(chineseStr, false, true, delimiter);
    }

    //汉字转拼音（携带音标）（携带分隔符）
    public static String toPinYinMark(String chineseStr, String delimiter) {
        return toPinYin(chineseStr, true, false, delimiter);
    }

    //汉字转拼音（携带音标）
    public static String toPinYinMark(String chineseStr) {
        return toPinYin(chineseStr, true, false, null);
    }

    //只保留汉字拼音（携带音标）
    public static String toPinYinMarkOnlyChinese(String chineseStr, String delimiter) {
        return toPinYin(chineseStr, true, true, delimiter);
    }

    //只保留汉字拼音（携带音标）
    public static String toPinYinMarkOnlyChinese(String chineseStr) {
        return toPinYin(chineseStr, true, true, null);
    }

    /**
     * 汉字转拼音
     *
     * @param chineseStr
     * @param hasMark
     * @param onlyChinese
     * @param delimiter
     * @return
     */
    public static String toPinYin(String chineseStr, boolean hasMark, boolean onlyChinese, String delimiter) {
        StringBuilder result = new StringBuilder();
        char[] chineseChars = chineseStr.trim().toCharArray();
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();

        if (hasMark) {
            format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
            format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        } else {
            format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            format.setVCharType(HanyuPinyinVCharType.WITH_V);
        }

        for (int i = 0; i < chineseChars.length; i++) {
            char chineseChar = chineseChars[i];
            // 如果字符是中文,则将中文转为汉语拼音
            boolean isChinese = String.valueOf(chineseChar).matches("[\u4e00-\u9fa5]+");
            if (isChinese) {
                try {
                    result.append(PinyinHelper.toHanyuPinyinStringArray(chineseChar, format)[0]);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    System.out.printf("字符[%s]不能转成汉语拼音%n", chineseChar);
                }
            }
            // 如果字符不是中文,则不转换
            else {
                if (!onlyChinese) {
                    result.append(chineseChar);
                }
            }

            if (delimiter != null && !(onlyChinese && !isChinese)) {
                result.append(delimiter);
            }
        }
        return result.toString();
    }


    /**
     * 每个汉字的首字母 大写
     */
    public static String eachFirstUpper(String chineseStr) {
        return eachFirst(chineseStr, HanyuPinyinCaseType.UPPERCASE, null);
    }

    public static String eachFirstUpper(String chineseStr, String delimiter) {
        return eachFirst(chineseStr, HanyuPinyinCaseType.UPPERCASE, delimiter);

    }

    /**
     * 每个汉字的首字母 小写
     */
    public static String eachFirstLower(String chineseStr) {
        return eachFirst(chineseStr, HanyuPinyinCaseType.LOWERCASE, null);
    }

    public static String eachFirstLower(String chineseStr, String delimiter) {
        return eachFirst(chineseStr, HanyuPinyinCaseType.LOWERCASE, delimiter);
    }

    /**
     * 每个汉字的首字母
     */
    public static String eachFirst(String chineseStr, HanyuPinyinCaseType caseType, String delimiter) {
        char[] chineseChars = chineseStr.trim().toCharArray();
        StringBuilder result = new StringBuilder();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(caseType);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        for (int i = 0; i < chineseChars.length; i++) {
            char chineseChar = chineseChars[i];

            String str = String.valueOf(chineseChar);

            if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
                try {
                    result.append(
                            PinyinHelper.toHanyuPinyinStringArray(chineseChar, defaultFormat)[0].charAt(0));
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    System.out.printf("字符[%s]不能转成汉语拼音%n", chineseChar);
                }
            }

            // 如果字符是数字,取数字
            else if (str.matches("\\d+")) {
                result.append(chineseChar);
            }

            // 如果字符是字母,取字母
            else if (str.matches("[a-zA-Z]+")) {
                result.append(chineseChar);
            }

            // 否则不转换
            else {
                // 如果是标点符号的话，带着
                result.append(chineseChar);
            }

            //添加分隔符
            if (i < chineseChars.length - 1 && delimiter != null) {
                result.append(delimiter);
            }
        }

        return result.toString();
    }


    /**
     * 第一个汉字的第一个字符
     */
    public static String first(String chineseStr) {
        char[] chineseChars = chineseStr.trim().toCharArray();
        StringBuilder result = new StringBuilder();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);// 输出拼音全部大写
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
        String str = String.valueOf(chineseChars[0]);
        if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
            try {
                result.append(PinyinHelper.toHanyuPinyinStringArray(chineseChars[0], defaultFormat)[0].substring(0, 1));
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                System.out.println(String.format("字符[%s]不能转成汉语拼音", chineseChars[0]));
            }
        } else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字
            result.append(chineseChars[0]);
        } else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母
            result.append(chineseChars[0]);
        } else {// 否则不转换

        }
        return result.toString();
    }

    public static void main(String[] args) {
        String chineseStr = "春眠不觉晓，处处闻啼鸟。";
        System.out.println(PinyinKit.first(chineseStr));
        System.out.println(PinyinKit.eachFirstLower(chineseStr));
        System.out.println(PinyinKit.eachFirstUpper(chineseStr));
        System.out.println();

        System.out.println(PinyinKit.first(chineseStr));
        System.out.println(PinyinKit.eachFirstLower(chineseStr, "*"));
        System.out.println(PinyinKit.eachFirstUpper(chineseStr, "*"));
        System.out.println();

        System.out.println(PinyinKit.toPinYin(chineseStr));
        System.out.println(PinyinKit.toPinYinOnlyChinese(chineseStr));
        System.out.println(PinyinKit.toPinYinMark(chineseStr));
        System.out.println(PinyinKit.toPinYinMarkOnlyChinese(chineseStr));
        System.out.println();

        System.out.println(PinyinKit.toPinYin(chineseStr, "-"));
        System.out.println(PinyinKit.toPinYinOnlyChinese(chineseStr, "-"));
        System.out.println(PinyinKit.toPinYinMark(chineseStr, "-"));
        System.out.println(PinyinKit.toPinYinMarkOnlyChinese(chineseStr, "-"));

    }

}
